Ext.define('Service.module.view.ModuleTree', {
    extend: 'Ext.tree.Panel',
    
    id : 'Service_module_view_ModuleTree',
    alias : 'widget.Service_module_view_ModuleTree',
    
    maxWidth: 400,
    minWidth: 200,
    title: '文件夹',
	
    addChildNode : function(record){
    	var me = this;
    	var pname = record.get('pname');
    	var moduleType = record.get('moduleType');
    	record.set('text',record.get('moduleText'));
    	if(moduleType == 2){
    		record.set('leaf',true);
    	}else{
    		record.set('expanded',true);
    	}
    	if(record.get('validFlag') != 1){
			if(moduleType == 1)
				record.set('iconCls','x-tree-icon-parent-invalid');
			else if(moduleType == 2)
				record.set('iconCls','x-tree-icon-leaf-invalid');
		}
		Ext.Ajax.request({
			url : Constants.REQUEST_URL,
			method : 'POST',
			params : {
				module : 'Service_module',
				action : 'getModuleIdByName',
				moduleName : pname
			},
			success : function(rep){
				var  data = Ext.decode(rep.responseText);
				var node = me.getStore().getNodeById(data['result']['id']);
		    	if(node){
		    		node.appendChild(record);
		    	}
			}
		});
    },
    
    chgNode : function(record){
    	var me = this;
    	var node = me.getStore().getNodeById(record.get('id'));
    	if(node && record){
    		node.data['moduleName'] = record.get('moduleName');
    		node.data['moduleText'] = record.get('moduleText');
    		node.data['text'] = record.get('moduleText');
    		node.data['serviceName'] = record.get('serviceName');
    		node.data['sortIndex'] = record.get('sortIndex');
    		node.data['bigIcon'] = record.get('bigIcon');
    		node.data['smallIcon'] = record.get('smallIcon');
    		node.data['remark'] = record.get('remark');
    		node.updateInfo({});
    	}
    },
    
    startModule : function(record){
    	var me = this;
    	Ext.Ajax.request({
			url : Constants.REQUEST_URL,
			method : 'POST',
			params : {
				module : 'Service_module',
				action : 'getModuleIdByName',
				moduleName : record.get('moduleName')
			},
			success : function(rep){
				var  data = Ext.decode(rep.responseText);
				var node = me.getStore().getNodeById(data['result']['id']);
		    	if(node && record){
		    		for(var i=0;i<10;i++){
		    			if(node==null || node.data['validFlag'] == 1 || node.data['moduleName'] == 'root')
		    				break;
		    			node.data['validFlag'] = 1;
						if(node.data['moduleType'] == 1)
							node.data.iconCls = 'x-tree-icon-parent';
						else if(node.data['moduleType'] == 2)
							node.data.iconCls = 'x-tree-icon-leaf';
						node.updateInfo({});
						node = node.parentNode;
					}
		    	}
			}
		});
    },
    
    stopModule : function(record){
    	var me = this;
    	Ext.Ajax.request({
			url : Constants.REQUEST_URL,
			method : 'POST',
			params : {
				module : 'Service_module',
				action : 'getModuleIdByName',
				moduleName : record.get('moduleName')
			},
			success : function(rep){
				var  data = Ext.decode(rep.responseText);
				var node = me.getStore().getNodeById(data['result']['id']);
		   		if(node && record){
		    		me.setInvalidNodes([node]);
		    	} 	
			}
		});
    },
    
    setInvalidNodes : function(nodes){
    	var me = this;
    	if(nodes == null || nodes.length==0)
    		return;
    	for(var i=0;i<nodes.length;i++){
    		var node = nodes[i];
    		if(node==null || node.data['validFlag'] == 0 || node.data['id']<=1){
    			continue;
    		}
    		node.data['validFlag'] = 0;
    		if(node.data['moduleType'] == 1)
				node.data.iconCls = 'x-tree-icon-parent-invalid';
			else if(node.data['moduleType'] == 2)
				node.data.iconCls = 'x-tree-icon-leaf-invalid';
				
			node.updateInfo({});
			
			if(node.hasChildNodes()){
				var nodes = node.childNodes;
				me.setInvalidNodes(nodes);
			}
    	}
    },
    
    delNode : function(record){
    	var me = this;
    	var node = me.getStore().getNodeById(record.get('id'));
    	if(node){
    		node.remove();
    	}
    },
    
    onItemContextMenu : function(v,r,it,i,e,o){
    	var me = this;
    	return me.onContextMenu(v,e,o);
    },
    
    initComponent: function() {
        var me = this;
        Ext.applyIf(me,{
        	selType : 'Service.module.model.ModuleTreeModel',
        	store : new Ext.data.TreeStore({
        		model : 'Service.module.model.ModuleModel',
        		proxy : {
        			type : 'memory'
        		},
        		root : {}
        	}),
            viewConfig : {
            
            },
            dockedItems: [
                {
                    xtype: 'toolbar',
                    dock: 'top',
                    items: [
                        {
                            xtype: 'button',
                            iconCls : 'module-expend-tree',
                            text: '全部展开',
                            handler : function(){
                            	 me.expandAll();
                            }
                        },
                        '-',
                        {
                            xtype: 'button',
                            iconCls : 'module-collapse-tree',
                            text: '全部关闭',
                            handler : function(){
                            	me.collapseAll();
                            }
                        }
                    ]
                }
            ]
        });
        me.callParent(arguments);
    }
});